using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;
using System.Windows.Forms;
using Athena.Objects;

namespace Athena.Tools
{
    public class ArrangeTool : INonVisualTool
    {
        public override bool Handles(List<Type> oTypes)
        {
            bool bResult = true;
            foreach (Type t in oTypes)
            {
                bResult &= Handles(t);
                if (!bResult) return false;
            }
            return bResult;
        }

        private bool Handles(Type t)
        {
            return Utility.IsTypeNoAbstract(typeof(Athena.Objects.IDrawable), t);
        }
        class ToolCommand
        {
            private string _sCommand;

            public string Command
            {
                get { return _sCommand; }
                set { _sCommand = value; }
            }

            private Bitmap _Icon;
            public Bitmap Icon
            {
                get
                {
                    return _Icon;
                }
                set
                {
                    _Icon = value;
                }
            }

            private string _Description;
            public string Description
            {
                get
                {
                    return _Description;
                }
                set
                {
                    _Description = value;
                }
            }


            public ToolCommand()
            {
                _sCommand = "-";
                _Icon = null;
            }

            /// <summary>
            /// Initializes a new instance of the ToolCommand class.
            /// </summary>
            /// <param name="sCommand"></param>
            /// <param name="icon"></param>
            /// <param name="description"></param>
            public ToolCommand(string sCommand, Bitmap icon, string description)
            {
                _sCommand = sCommand;
                _Icon = icon;
                _Description = description;
            }

            public ToolStripItem ToMenuItem()
            {
                if (_Icon == null)
                {
                    return new ToolStripSeparator();
                }
                else
                {

                    ToolStripMenuItem oItem = new ToolStripMenuItem(_sCommand, _Icon);
                    oItem.Tag = _sCommand;
                    oItem.ToolTipText = _Description;
                    oItem.ImageScaling = ToolStripItemImageScaling.None;
                    oItem.Click += new EventHandler(CommandExecute);
                    return oItem;
                }
            }

            public ToolStripItem ToToolStripButton()
            {
                if (_Icon == null)
                {
                    return new ToolStripSeparator();
                }
                else
                {
                    ToolStripButton oButton = new ToolStripButton(_sCommand, _Icon);
                    oButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
                    oButton.Tag = _sCommand;
                    oButton.ImageScaling = ToolStripItemImageScaling.None;
                    oButton.ToolTipText = _Description;
                    oButton.Click += new EventHandler(CommandExecute);
                    return oButton;
                }
            }

            void CommandExecute(object sender, EventArgs e)
            {
                string sTag = "";
                if (sender is ToolStripButton)
                    sTag = (string)((ToolStripButton)sender).Tag;
                else if (sender is ToolStripMenuItem)
                    sTag = (string)((ToolStripMenuItem)sender).Tag;
                else
                    return;
                switch (sTag)
                {
                    case "Bring To Front":
                        Utility.BringItemsToFront(CurrentModel.Selected, CurrentModel);
                        break;
                    case "Send To Back":
                        Utility.SendItemsToBack(CurrentModel.Selected, CurrentModel);
                        break;
                    case "Bring Forward":
                        Utility.BringItemsForward(CurrentModel.Selected, CurrentModel);
                        break;
                    case "Send Backward":
                        Utility.SendItemsBackward(CurrentModel.Selected, CurrentModel);
                        break;
                    default:
                        break;
                }
            }


        }

        static ToolCommand[] _oCommands = new ToolCommand[]
        {
            new ToolCommand("Bring To Front", global::Athena.Properties.Resources.ICON_BringToFront, "Brings the selected elements to the front."),
            new ToolCommand("Send To Back", global::Athena.Properties.Resources.ICON_SendToBack, "Sends the selected elements to the back."),
            new ToolCommand(), 
            new ToolCommand("Bring Forward", global::Athena.Properties.Resources.ICON_BringForward, "Brings the selected elements one level forward."),
            new ToolCommand("Send Backward", global::Athena.Properties.Resources.ICON_SendBackward, "Sends the selected elements one level backward."),
        };

        private static ArrangeTool _oInstance = new ArrangeTool();
        public static ArrangeTool Instance
        {
            get { return _oInstance; }
        }

        private ArrangeTool()
        {
            // no items to put into global tool window
            _bHasMenuItems = false;
            // has items to put into context menu
            _bHasContextMenuItems = true;
            // needs a separate toolstrip for the arrange actions
            _bRequiresNewToolWindow = true;


            // register with main instance
            ToolManager.Instance.RegisterTool("Arrange Tool", "Arranges selected elements on the screen.", this);
        }


        public override void SetupToolStrip(ToolStrip oStrip)
        {
            foreach (ToolCommand command in _oCommands)
            {
                oStrip.Items.Add(command.ToToolStripButton());
            }
        }
        public override void SetupContextMenu(ToolStripMenuItem oStrip)
        {
            foreach (ToolCommand command in _oCommands)
            {
                oStrip.DropDownItems.Add(command.ToMenuItem());
            }
        }
        /*
        public static void AutoArrange(List<IDrawable> drawables)
        {
            RectangleF bounds = Utility.GetBounds(drawables);
            foreach (IDrawable id in drawables)
            {
                if (id is IGlyph)
                {
                    foreach (IDrawable id2 in drawables)
                    {
                        if (id2 is IGlyph)
                        {
                            float dist = Utility
                            id.Location
                        }
                    }
                }
            }
        }*/

    }
}
